
package com.insigma.business.qrcode.merchant.service.impl;

import com.insigma.business.qrcode.common.service.SequenceGenerator;
import com.insigma.business.qrcode.common.util.AdminAreaCodeUtils;
import com.insigma.business.qrcode.soldier.dto.SequenceNameConstant;
import com.insigma.business.qrcode.soldier.entity.MerchantUserDO;
import com.insigma.business.qrcode.merchant.dao.CareWelfareMerchantInfoDao;
import com.insigma.business.qrcode.welfare.dao.MerchantUserDao;
import com.insigma.business.qrcode.soldier.entity.DataStatsCDO;
import com.insigma.business.qrcode.welfare.dto.CareWelfareMerchantBO;
import com.insigma.business.qrcode.merchant.dto.search.CareWelfareMerchantInfoBSearcher;
import com.insigma.business.qrcode.merchant.entity.CareWelfareMerchantInfoDO;
import com.insigma.business.qrcode.merchant.service.CareWelfareMerchantInfoService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service("careWelfareMerchantInfoService")
public class CareWelfareMerchantInfoServiceImpl implements CareWelfareMerchantInfoService {

    @Autowired(required = false)
    private CareWelfareMerchantInfoDao careWelfareMerchantInfoDao;
    @Autowired(required = false)
    private SequenceGenerator sequenceGenerator;

    @Autowired(required = false)
    private MerchantUserDao merchantUserDao;

    
    @Override
    public List<CareWelfareMerchantInfoDO> search(final CareWelfareMerchantInfoBSearcher careWelfareMerchantInfoBSearcher) {
        String adminAreaCode = careWelfareMerchantInfoBSearcher.getAdminAreaCode();
        if (Strings.isNotBlank(adminAreaCode)) {
            careWelfareMerchantInfoBSearcher.setAdminAreaCode(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode));
        }
        if (careWelfareMerchantInfoBSearcher.getShow() == null) {
            careWelfareMerchantInfoBSearcher.setShow(true);
        }
        List<CareWelfareMerchantInfoDO> list = careWelfareMerchantInfoDao.search(careWelfareMerchantInfoBSearcher);
        return list;
    }
   
    @Override
    public CareWelfareMerchantInfoDO get(final String id){
        return careWelfareMerchantInfoDao.get(id);
    }

    
    @Override
    public Integer insert(final CareWelfareMerchantInfoDO careWelfareMerchantInfoDO){
        careWelfareMerchantInfoDO.setId(sequenceGenerator.genKey(SequenceNameConstant.CARE_WELFARE_MERCHANT_INFO_ID));
        if (careWelfareMerchantInfoDO.getShow() == null) {
            careWelfareMerchantInfoDO.setShow(true);
        }
        return careWelfareMerchantInfoDao.insert(careWelfareMerchantInfoDO);
    }

    
    @Override
    public Integer update(final CareWelfareMerchantInfoDO careWelfareMerchantInfoDO){
        if (careWelfareMerchantInfoDO.getShow() == null) {
            careWelfareMerchantInfoDO.setShow(true);
        }
        return careWelfareMerchantInfoDao.update(careWelfareMerchantInfoDO);
    }

    
    @Override
    public Integer delete(final String id, final String modifierType, final String modifierId){
        return careWelfareMerchantInfoDao.delete(id,modifierType,modifierId);
    }

    @Override
    public List<CareWelfareMerchantBO> searchMerchantInfo(CareWelfareMerchantInfoBSearcher searcher) {
        if (searcher.getShow() == null) {
            searcher.setShow(true);
        }
        return careWelfareMerchantInfoDao.searchMerchantInfo(searcher);
    }

    @Override
    public CareWelfareMerchantInfoDO getByMerchantQrString(String qrString) {
        return careWelfareMerchantInfoDao.getByMerchantQrString(qrString);
    }

    @Override
    public List<CareWelfareMerchantBO> searchAllMerchantInfo(CareWelfareMerchantInfoBSearcher searcher) {
        if (searcher.getShow() == null) {
            searcher.setShow(true);
        }

        return careWelfareMerchantInfoDao.searchAllMerchantInfo(searcher);
    }

    @Override
    public int countMerchant(CareWelfareMerchantInfoBSearcher searcher) {
        if (searcher.getShow() == null) {
            searcher.setShow(true);
        }

        return careWelfareMerchantInfoDao.countMerchant(searcher);
    }

    @Override
    public int countMerchantScreen(String areaLike) {
        return careWelfareMerchantInfoDao.countMerchantScreen(areaLike);
    }

    @Override
    public List<DataStatsCDO> countMerchantType() {
        return careWelfareMerchantInfoDao.countMerchantType();
    }

    @Override
    public List<DataStatsCDO> countMerchantTypeAndArea(String adminAreaCode) {
        return careWelfareMerchantInfoDao.countMerchantTypeAndArea(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode));
    }

    @Override
    public List<DataStatsCDO> countMerchantTypeTraffic() {
        return careWelfareMerchantInfoDao.countMerchantTypeTraffic();
    }

    @Override
    public List<DataStatsCDO> countMerchantTopTraffic() {
        return careWelfareMerchantInfoDao.countMerchantTopTraffic();
    }

    @Override
    public int countMerchantTraffic() {
        return careWelfareMerchantInfoDao.countMerchantTraffic();
    }

    @Override
    public List<DataStatsCDO> queryHzAreaMerchantDetail() {
        return careWelfareMerchantInfoDao.queryHzAreaMerchantDetail(null, null, null);
    }

    @Override
    public List<DataStatsCDO> queryHzAreaMerchantDetail(Date beginDate, Date endDate, Boolean show) {
        return careWelfareMerchantInfoDao.queryHzAreaMerchantDetail(beginDate, endDate, show);
    }

    @Override
    public String getMaxCode(String code) {
        return careWelfareMerchantInfoDao.getMaxCode(code + "%");
    }

    @Override
    public List<CareWelfareMerchantInfoDO> listByStreetId(String streetId) {
        return careWelfareMerchantInfoDao.listByStreetId(streetId);
    }

    @Override
    public String getAliAccountIdById(String id) {
        MerchantUserDO byMerchantId = merchantUserDao.getByMerchantId(id);
        if (byMerchantId != null) {
            return byMerchantId.getOuterAccountId();
        }

        return "";
    }

    @Override
    public CareWelfareMerchantInfoDO getByEntryId(String entryId) {
        return careWelfareMerchantInfoDao.getByEntryId(entryId);
    }

    @Override
    public Integer countByAreaAndDate(String adminAreaCode, Date startDate, Date endDate) {
        return careWelfareMerchantInfoDao.countByAreaAndDate(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode), startDate, endDate);
    }

    @Override
    public List<DataStatsCDO> countMerchantTopTrafficByArea(String adminAreaCode) {
        return careWelfareMerchantInfoDao.countMerchantTopTrafficByArea(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode));
    }

    @Override
    public List<String> listAllPublishedMerchantId() {
        return careWelfareMerchantInfoDao.listAllPublishedMerchantId();
    }

    @Override
    public List<DataStatsCDO> groupScanRecord(String adminAreaCode) {
        return careWelfareMerchantInfoDao.groupScanRecord(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode));
    }

    @Override
    public List<CareWelfareMerchantInfoDO> getByOuterSysId(String outerSysAccountId) {
        return careWelfareMerchantInfoDao.getByOuterSysId(outerSysAccountId);
    }


}
